﻿using System;
using System.Collections.Generic;
using System.Text;

using Plumber.Database;

using ApplicationLibrary;
using ApplicationLibrary.SqlConnector;

namespace Plumber.Database
{
    class RegisterModule : RegisterTable
    {
        protected override string Module()
        {
            return "Plumber";
        }

        protected override int CurrentVersion()
        {
            return 3;
        }

        public override int Code()
        {
            return 2;
        }

        public override Version DllVersion
        {
            get
            {
                return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
            }
        }

        public override void RegisterCountersAndCodes()
        {
            RRMasterType rrMasterType = new RRMasterType();
            rrMasterType.Find();

            for (int t = 0; t < rrMasterType.Count; t++)
                GlobalInfo.AddCodeType(rrMasterType.GetStringColumn(PL_MasterType.Code, t), rrMasterType.GetStringColumn(PL_MasterType.Description,t));

            GlobalInfo.AddCounter((int)Counters.SaleInvoice, Properties.Resources.CN_SaleInvoices);
            GlobalInfo.AddCounter((int)Counters.JournalEntry, Properties.Resources.CN_JournalEntry);
        }

        protected override bool CreateDBTables()
        {
            SqlCreateTable newTable = new SqlCreateTable(SqlABConnection);

            #region Masters
            #region PL_Masters
            if (SearchTable(PL_Masters.Name))
            {
                newTable.NewTable(PL_Masters.Name);
                newTable.AddColumn(PL_Masters.Type);
                newTable.AddColumn(PL_Masters.Code);
                newTable.AddColumn(PL_Masters.Description);
                newTable.AddColumn(PL_Masters.Address);
                newTable.AddColumn(PL_Masters.ZipCode);
                newTable.AddColumn(PL_Masters.City);
                newTable.AddColumn(PL_Masters.County);
                newTable.AddColumn(PL_Masters.Phone1);
                newTable.AddColumn(PL_Masters.Phone2);
                newTable.AddColumn(PL_Masters.Fax);
                newTable.AddColumn(PL_Masters.Refer1);
                newTable.AddColumn(PL_Masters.Refer2);
                newTable.AddColumn(PL_Masters.Title);

                newTable.AddColumn(PL_Masters.TaxNo);
                newTable.AddColumn(PL_Masters.FiscalCode);
                newTable.AddColumn(PL_Masters.Administrator);

                newTable.AddColumn(PL_Masters.Disabled);
                newTable.AddColumn(PL_Masters.Blocked);

                newTable.AddPrimaryKey(PL_Masters.Type, PL_Masters.Code);

                newTable.Create();
            }
            #endregion
            #region PL_Titles
            if (SearchTable(PL_Titles.Name))
            {
                newTable.NewTable(PL_Titles.Name);

                newTable.AddColumn(PL_Titles.Title);
                newTable.AddColumn(PL_Titles.Description);

                newTable.AddPrimaryKey(PL_Titles.Title);

                newTable.Create();
            }
            #endregion 
            #region PL_MasterType
            if (SearchTable(PL_MasterType.Name))
            {
                newTable.NewTable(PL_MasterType.Name);

                newTable.AddColumn(PL_MasterType.Code);
                newTable.AddColumn(PL_MasterType.Description);
                newTable.AddColumn(PL_MasterType.CodeMode);
                newTable.AddColumn(PL_MasterType.MasterType);
                newTable.AddColumn(PL_MasterType.AccountType);

                newTable.AddPrimaryKey(PL_MasterType.Code);

                newTable.Create();
            }
            #endregion 
            #endregion
            #region Sales
            #region PL_SalesInvoices
            if (SearchTable(PL_SalesInvoices.Name))
            {
                newTable.NewTable(PL_SalesInvoices.Name);
                newTable.AddColumn(PL_SalesInvoices.InvoiceNo);
                newTable.AddColumn(PL_SalesInvoices.ReleaseDate);
                newTable.AddColumn(PL_SalesInvoices.Type);
                newTable.AddColumn(PL_SalesInvoices.Master);
                newTable.AddColumn(PL_SalesInvoices.Title);
                newTable.AddColumn(PL_SalesInvoices.Description);
                newTable.AddColumn(PL_SalesInvoices.Address);
                newTable.AddColumn(PL_SalesInvoices.ZipCode);
                newTable.AddColumn(PL_SalesInvoices.City);
                newTable.AddColumn(PL_SalesInvoices.County);
                newTable.AddColumn(PL_SalesInvoices.TaxNo);
                newTable.AddColumn(PL_SalesInvoices.FiscalCode);
                newTable.AddColumn(PL_SalesInvoices.Administrator);
                newTable.AddColumn(PL_SalesInvoices.InvoiceBody);
                newTable.AddColumn(PL_SalesInvoices.Amount);
                newTable.AddColumn(PL_SalesInvoices.Total);
                newTable.AddColumn(PL_SalesInvoices.Tax);
                newTable.AddColumn(PL_SalesInvoices.TaxCode);

                newTable.AddColumn(PL_SalesInvoices.PaymentDate);
                newTable.AddColumn(PL_SalesInvoices.SalePayed);
                newTable.AddColumn(PL_SalesInvoices.PrintEnvelope);

                newTable.AddColumn(PL_SalesInvoices.Restraint);
                newTable.AddColumn(PL_SalesInvoices.RestraintCode);
                newTable.AddColumn(PL_SalesInvoices.RestraintAmount);

                newTable.AddPrimaryKey(PL_SalesInvoices.InvoiceNo);

                newTable.Create();
            }
            #endregion
            #region PL_Sentences
            if (SearchTable(PL_Sentences.Name))
            {
                newTable.NewTable(PL_Sentences.Name);

                newTable.AddColumn(PL_Sentences.ID);
                newTable.AddColumn(PL_Sentences.Title);
                newTable.AddColumn(PL_Sentences.Text);

                newTable.AddPrimaryKey(PL_Sentences.ID);

                newTable.Create();
            }
            #endregion
            #region PL_Templates
            if (SearchTable(PL_Templates.Name))
            {
                newTable.NewTable(PL_Templates.Name);

                newTable.AddColumn(PL_Templates.Code);
                newTable.AddColumn(PL_Templates.Description);
                newTable.AddColumn(PL_Templates.Body);
                newTable.AddColumn(PL_Templates.Combine);

                newTable.AddPrimaryKey(PL_Templates.Code);

                newTable.Create();
            }
            #endregion 
            #endregion
            #region Accounting
            #region PL_JournalEntry
            if (SearchTable(PL_JournalEntry.Name))
            {
                newTable.NewTable(PL_JournalEntry.Name);

                newTable.AddColumn(PL_JournalEntry.JournalNo);
                newTable.AddColumn(PL_JournalEntry.Template);
                newTable.AddColumn(PL_JournalEntry.PostingDate);
                newTable.AddColumn(PL_JournalEntry.Description);

                newTable.AddPrimaryKey(PL_JournalEntry.JournalNo);

                newTable.Create();
            }
            #endregion
            #region PL_JournalEntryDetail
            if (SearchTable(PL_JournalEntryDetail.Name))
            {
                newTable.NewTable(PL_JournalEntryDetail.Name);

                newTable.AddColumn(PL_JournalEntryDetail.JournalNo);
                newTable.AddColumn(PL_JournalEntryDetail.Line);
                newTable.AddColumn(PL_JournalEntryDetail.FromAccountType);
                newTable.AddColumn(PL_JournalEntryDetail.FromMasterCode);
                newTable.AddColumn(PL_JournalEntryDetail.FromAmount);
                newTable.AddColumn(PL_JournalEntryDetail.ToAccountType);
                newTable.AddColumn(PL_JournalEntryDetail.ToMasterCode);
                newTable.AddColumn(PL_JournalEntryDetail.ToAmount);
                newTable.AddColumn(PL_JournalEntryDetail.Description);

                newTable.AddPrimaryKey(PL_JournalEntry.JournalNo, PL_JournalEntryDetail.Line);

                newTable.Create();
            }
            #endregion
            #endregion
            return true;
        }

        protected override bool UpdateDBTables()
        {
            if (dbVersion < 2)
            {
                SqlCreateTable alterMaster = new SqlCreateTable(SqlABConnection);

                if (SearchColumn(PL_Masters.Phone1))
                {
                    alterMaster.AlterTable(PL_Masters.Name);
                    alterMaster.AlterColumn(PL_Masters.Phone1);
                    alterMaster.Alter();
                }

                if (SearchColumn(PL_Masters.Phone2))
                {
                    alterMaster.AlterTable(PL_Masters.Name);
                    alterMaster.AlterColumn(PL_Masters.Phone2);
                    alterMaster.Alter();
                }

                if (SearchColumn(PL_Masters.Fax))
                {
                    alterMaster.AlterTable(PL_Masters.Name);
                    alterMaster.AlterColumn(PL_Masters.Fax);
                    alterMaster.Alter();
                }

                if (!SearchColumn(PL_SalesInvoices.PaymentDate))
                {
                    alterMaster.AlterTable(PL_SalesInvoices.Name);
                    alterMaster.AddColumn(PL_SalesInvoices.PaymentDate);
                    alterMaster.Alter();
                }

                if (!SearchColumn(PL_SalesInvoices.SalePayed))
                {
                    alterMaster.AlterTable(PL_SalesInvoices.Name);
                    alterMaster.AddColumn(PL_SalesInvoices.SalePayed);
                    alterMaster.Alter();
                }
            }

            if (dbVersion < 3)
            {
                SqlCreateTable alterMaster = new SqlCreateTable(SqlABConnection);

                if (SearchColumn(PL_SalesInvoices.Address))
                {
                    alterMaster.AlterTable(PL_SalesInvoices.Name);
                    alterMaster.AlterColumn(PL_SalesInvoices.Address);
                    alterMaster.Alter();
                }

                if (SearchColumn(PL_Masters.Address))
                {
                    alterMaster.AlterTable(PL_Masters.Name);
                    alterMaster.AlterColumn(PL_Masters.Address);
                    alterMaster.Alter();
                }

                if (!SearchColumn(PL_SalesInvoices.Title))
                {
                    alterMaster.AlterTable(PL_SalesInvoices.Name);
                    alterMaster.AddColumn(PL_SalesInvoices.Title);
                    alterMaster.Alter();
                }

                if (!SearchColumn(PL_Masters.Title))
                {
                    alterMaster.AlterTable(PL_Masters.Name);
                    alterMaster.AddColumn(PL_Masters.Title);
                    alterMaster.Alter();

                }

                if (!SearchColumn(PL_SalesInvoices.Restraint))
                {
                    alterMaster.AlterTable(PL_SalesInvoices.Name);
                    alterMaster.AddColumn(PL_SalesInvoices.Restraint);
                    alterMaster.Alter();
                }

                if (!SearchColumn(PL_SalesInvoices.RestraintCode))
                {
                    alterMaster.AlterTable(PL_SalesInvoices.Name);
                    alterMaster.AddColumn(PL_SalesInvoices.RestraintCode);
                    alterMaster.Alter();
                }

                if (!SearchColumn(PL_SalesInvoices.RestraintAmount))
                {
                    alterMaster.AlterTable(PL_SalesInvoices.Name);
                    alterMaster.AddColumn(PL_SalesInvoices.RestraintAmount);
                    alterMaster.Alter();
                }
            }
            //if (dbVersion < 4)
            //{
            //    SqlCreateTable alterInvoice = new SqlCreateTable(SqlABConnection);

            //    if (!SearchColumn(PL_SalesInvoices.PrintEnvelope))
            //    {
            //        alterInvoice.AlterTable(PL_SalesInvoices.Name);
            //        alterInvoice.AddColumn(PL_SalesInvoices.PrintEnvelope);
            //        alterInvoice.Alter();
            //    }
            //}
            return true;
        }
    }
}
